; Generated by Modix - Version 3.6.2 Macro A

M104 T1 S0 								; turn off heater
M104 T0 S0 								; turn off heater
if heat.heaters[1].current > 45			; check for heater temp
	M291 S3 R"Attention!" P"Temperature is above 45c. Printer will wait until the temperature has dropped to below 45."
	M106 P1 S255 						; turn on fan to help cool
else
	G4 P0								; do nothing

while heat.heaters[1].current > 45 		; wait for the heater to be under 30 degrees
	G4 S2								; loop wait 2 seconds
	
M106 P1 S0 								; turn fan off before tuning

G28										; home all
T1 R-1									; select T1
G1 X200 Y200 F3000						; move to center area of the bed
G1 Z2 									; move nozzle down

M300 S666 P666 							; beep
M291 S3 R"Attention!" P"During auto tuning there is minimal protection against heating faults. Therefore you should not leave the printer unattended"
M570 H1 P120 T50						; Extend the heater H1 fault timeout to 120 seconds for testing
M291 T0 S4 R"PID temperature" P"Select the temperature you wish to run the PID tuning at." K{"210","230","250","260","270"} 
	if input = 0
		M303 H1 T1 P1 S210 F1.0 Q1				; Start the PID tune at 210c, fan at 100%, Do not display messages
	if input = 1
		M303 H1 T1 P1 S230 F1.0 Q1				; Start the PID tune at 230c, fan at 100%, Do not display messages
	if input = 2
		M303 H1 T1 P1 S250 F1.0 Q1				; Start the PID tune at 250c, fan at 100%, Do not display messages
	if input = 3
		M303 H1 T1 P1 S260 F1.0 Q1				; Start the PID tune at 260c, fan at 100%, Do not display messages
	if input = 4
		M303 H1 T1 P1 S270 F1.0 Q1				; Start the PID tune at 270c, fan at 100%, Do not display messages

G4 S1									; wait 1 second						
while heat.heaters[1].state = "tuning"	; wait for the PID tune to be completed
     G4 S1								; loop wait 1 second
M291 S3 R"The results of the PID tune have been automatically saved" P"You can view the results of the PID tune in the PID_tune_E1.g file"

echo "The results of the PID tune have been automatically saved." 
echo "You can view the results of the PID tune in the PID_tune_E1.g file"
echo >"0:/sys/PID_tune_E1.g" "M307 H1 R"^{heat.heaters[1].model.heatingRate}, "K"^{heat.heaters[1].model.coolingRate}^":"^{heat.heaters[1].model.fanCoolingRate}, "D"^{heat.heaters[1].model.deadTime}, "E"^{heat.heaters[1].model.coolingExp}
echo >>"0:/sys/PID_tune_E1.g" "M307 H1 B0 S1.0 V"^{heat.heaters[1].model.standardVoltage}

M570 H1 P5 T15							; set heater fault detection back to default
G1 Z10									; move nozzle up
M84										; disable stepper motors